// pages/giftShare/giftShare.js
const app = getApp()
var api = require('../../../utils/api.js')
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    card: [],
    total_num: 0,
    getCardSuccess: false,
    addressCol: true,
    masklayerCol: true,
    addressCol: true,
    numCol: false,
    current_num: 1,
    useCol:true,
    phoneCol:true,
    machineNum: '', //机柜号

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var sku_code = options.sku_code;
    this.setData({
      sku_code: sku_code
    })
  },
  onShow:function(){
    this.goodsInfo()
    this.addressList()
  },
  //空操作
  emptyFunction:function(){

  },
  //商品信息
  goodsInfo: function () {
    var that = this;
    api.POST({
      params: {
        sku_code: that.data.sku_code
      },
      url: app.globalData.base_url + 'winecabinet/info',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          that.setData({
            goods: res.data.data
          })
        } else {
          util.showError(res.data.message);
        }
      },
      fail: function (fail) {
        util.showError("请检查网络");
      }
    })
  },

  //配送
  peisong: function () {
    this.setData({
      masklayerCol: false,
      numCol: false,
      addressCol:true,
      current_num:1
    })
  },
  //隐藏弹窗
  hiddenMask:function(){
    this.setData({
      masklayerCol: true,
      numCol: true,
      addressCol: true,
      current_num: 1
    })
  },

  // 数量确定
  numConfirm: function () {
    var that = this;
    var current_num = that.data.current_num;
    if (current_num > 0) {
      this.setData({
        numCol: true,
        addressCol: false
      })
    } else {
      util.showError("请选择数量");
    }

  },

  //隐藏地址
  hiddenAddress: function () {
    this.setData({
      masklayerCol: true,
      addressCol: true
    })
  },
  //增加
  addNum: function (e) {
    var that = this;
    var goods = that.data.goods;
    var current_num = that.data.current_num;
    if (current_num < goods.num) {
      current_num += 1;
      that.setData({
        current_num: current_num
      })
    }
  },
  //减少
  minusNum: function (e) {
    var that = this;
    var current_num = that.data.current_num;
    if (current_num >= 1) {
      current_num -= 1;
      that.setData({
        current_num: current_num
      })
    }
  },

  //地址列表
  addressList: function () {
    var params = new Object()
    var that = this;
    api.POST({
      params: params,
      url: app.globalData.base_url + 'address/list',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          var addressList = res.data.data.address_list;
          for (var i = 0; i < addressList.length; i++) {
            if (addressList[i].is_default == 1) {
              var addressId = addressList[i].aid
            }
          }
          that.setData({
            addressId: addressId,
            address: addressList
          })
        }
      },
      fail: function (fail) {
        util.showError("网络错误，请检查网络");
      }
    })
  },

  //添加新地址
  addAddress: function () {
    wx.navigateTo({
      url: '../addAddress/addAddress'
    })
  },

  //修改地址
  editorAddress: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/addAddress/addAddress?aid=' + id
    })
  },
  //删除地址
  deleteAddress: function (e) {
    var id = e.currentTarget.dataset.id;
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确认删除该地址?',
      confirmColor: "#e51927",
      success: function (res) {
        if (res.confirm) {
          api.POST({
            params: { aid: id },
            url: app.globalData.base_url + 'address/del',
            success: function (res) {
              if (util.isEmpty(res.data.code)) {
                util.showStatus("删除成功")
                that.addressList();
              }
            },
            fail: function (fail) {
              util.showError("网络错误，请检查网络");
            }
          })
        } else if (res.cancel) {
          // console.log('用户点击取消')
        }
      }
    })
  },

  //选择地址
  radioChange: function (e) {
    var id = e.detail.value
    this.setData({
      addressId: e.detail.value
    })
  },

  //确认配送
  confirm: function (e) {
    var that = this;
    var goods = that.data.goods;
    var params = new Object()
    params.record = [];
    var list = [];
    var box = {};
    box.sku_code = goods.sku_code
    box.num = that.data.current_num
    list.push(box);
    params.record = JSON.stringify(list)
    params.use_type = 2;
    params.aid = that.data.addressId;
    api.POST({
      params: params,
      url: app.globalData.base_url + 'winecabinet/use',
      success: function (res) {
        if (res.data.code) {
          util.showError(res.data.message)
        } else {
          wx.showModal({
            title: '提示',
            content: '提交成功',
            cancelText: '查看订单',
            confirmText: '返回酒柜',
            success: function (res) {
              if (res.confirm) {
                wx.switchTab({
                  url: '../wineNew/wineNew'
                })
              } else if (res.cancel) {
                wx.redirectTo({
                  url: '../myOrder/myOrder'
                })
              }
            }
          })
        }
      }, fail: function (fail) {
        util.showError("请检查网络");
      }
    })
  },
  shopAddress:function(){
    var coupons_code = this.data.sku_code;
    wx.navigateTo({
      url: '../shopAddress/shopAddress?coupons_code=' + coupons_code
    })
  },

  //---------------------立即使用----------------------------

  useGoods:function(e){
    this.setData({
      useCol:false,
      current_num: 1
    })
  },
  //隐藏立即使用弹框
  hiddenUseGoods:function(){
    this.setData({
      useCol: true,
      current_num: 1
    })
  },

  //提交
  confirmOrder: function () {
    var current_num = this.data.current_num;
    if (current_num >= 1) {
      this.setData({
        phoneCol: false
      })
    } else {
      util.showError("请选择商品数量");
    }
  },

  //输入柜机号弹框
  machineNum: function (e) {
    this.setData({
      machineNum: e.detail.value.replace(/\s+/g, '')
    })
  },
  //隐藏输入柜机号弹框
  hiddenPhone: function () {
    this.setData({
      phoneCol: true,
      machineNum: ''
    })
  },

  //确认配送
  submit: function (e) {
    var pickup_type = e.currentTarget.dataset.type
    var that = this;
    var params = new Object()
    params.use_type = 1;
    if (pickup_type == 1) {
      var current_num = this.data.current_num;
      if (current_num < 1) {
        util.showError("请选择商品数量");
        return;
      }
      params.pickup_type = 1
    } else {
      if (that.data.machineNum == '') {
        util.showError("请输入机柜号");
        return;
      } else {
        params.pickup_type = 2;
        params.machine_number = that.data.machineNum;
      }
    }
    var list = [];
    var box = {
      sku_code: that.data.goods.sku_code,
      num: that.data.current_num
    };
    list.push(box);
    params.record = JSON.stringify(list)

    api.POST({
      params: params,
      url: app.globalData.base_url + 'winecabinet/use',
      success: function (res) {
        if (res.data.code) {
          util.showError(res.data.message)
        } else {
          that.setData({
            phoneCol: true,
            useCol:true,
            machineNum: ''
          })

          if (pickup_type == 1) {
            var qrcode_image = res.data.data.result.qrcode_image
            wx.redirectTo({
              url: '../pickUpCode/pickUpCode?qrcode_image=' + qrcode_image,
            })
          } else if (pickup_type == 2) {
            var pickupCabinetNumber = res.data.data.result.pickupCabinetNumber
            wx.redirectTo({
              url: '../pickUpSuccessful/pickUpSuccessful?pickupCabinetNumber=' + JSON.stringify(pickupCabinetNumber),
            })
          }
        }
      }, fail: function (fail) {
        util.showError("网络错误");
      }
    })
  },
})